const { Worker, isMainThread, parentPort, workerData, table } = require('worker_threads');
const path = require('path');
const { model, run } = require('./WorkGRA.js')
// const WorkGW = require('./WorkGW.js')
const worker = {
    // 分割数据
    splitData(data, numChunks) {
        const chunks = [];
        const chunkSize = Math.ceil(data.length / numChunks);
        for (let i = 0; i < data.length; i += chunkSize) {
            chunks.push(data.slice(i, i + chunkSize));
        }
        return chunks;
    },
    // 插入数据
    insertFun: async (data, table) => {
        let chunks = null;
        if (data.length > 100) {
            chunks = worker.splitData(data, 10);
        } else {
            chunks = worker.splitData(data, 5);
        }
        let curPath = path.resolve(__dirname, 'WorkGRA.js').replace(/\\/g, '/');
        console.log(curPath, path.resolve(__dirname), path.basename(__filename))
        run();
        if (/graph$/.test(table)) {
            curPath = path.resolve(__dirname, 'WorkGRA.js').replace(/\\/g, '/');
        }
        if (/gw$/.test(table)) {
            curPath = path.resolve(__dirname, 'WorkGW.js').replace(/\\/g, '/');
        }
        for (let i = 0; i < chunks.length; i++) {
            // console.log(table)
            new Worker(curPath, { workerData: { data: chunks[i], table } });
        }
    }
}

module.exports = worker
